<template>
  <div class="student-container">
    <van-nav-bar
      fixed
      left-arrow
      @click-left="
        $router.back();
        socket.disconnect();
      "
      title="小智同学"
    ></van-nav-bar>
    <div class="chat-list" ref="name">
      <template v-for="item in message">
        <!-- 左侧是机器人小智 -->
        <div
          class="chat-item left"
          v-if="item.isStu"
          :key="item.timestamp.toString()"
        >
          <van-image fit="cover" round :src="left" />
          <div class="chat-pao">{{ item.msg }}</div>
        </div>
        <!-- 右侧是当前用户 -->
        <div class="chat-item right" v-else :key="item.timestamp.toString()">
          <div class="chat-pao my">{{ item.msg }}</div>
          <van-image fit="cover" :src="right" round />
        </div>
      </template>
    </div>
    <div class="reply-container van-hairline--top">
      <van-search placeholder="说点什么..." v-model="value" @search="onSearch">
      </van-search>
    </div>
  </div>
</template>

<script>
import left from "@/assets/1.png";
import right from "@/assets/2.png";
import io from "socket.io-client";
export default {
  name: "student",
  data() {
    return {
      left,
      right,
      value: "",
      socket: null,
      message: [] //聊天信息
    };
  },
  created() {
    this.socket = io("http://118.24.62.36:8888");
    this.socket.on("content", () => {
      console.log("建立连接成功了");
    });
    this.socket.on("message", data => {
      // console.log(data);
      this.message.push({
        msg: data.msg,
        timestamp: Date.now(),
        isStu: true
      });
    });
  },
  methods: {
    onSearch() {
      if (this.value == "") {
        this.$dialog.alert({
          title: "友情提示",
          message: "请输入点什么吧？"
        });
        return;
      }
      this.socket.emit("message", {
        msg: this.value,
        // 时间戳
        timestamp: Date.now()
      });
      this.timestamp = Date.now();
      this.message.push({
        msg: this.value,
        timestamp: Date.now(),
        isStu: false
      });
      this.value = "";
      // 自动滚动
    }
  },
  updated() {
    this.$nextTick(function() {
      this.$refs.name.scrollTop = this.$refs.name.scrollHeight;
    });
  }
};
</script>

<style lang="less">
.student-container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background: #fafafa;
  padding: 46px 0 50px 0;
  .van-nav-bar {
    background-color: #3296fa;
    .van-nav-bar__title {
      color: white;
    }
    .van-icon {
      color: white;
    }
  }
  .chat-list {
    height: 100%;
    overflow-y: scroll;
    .chat-item {
      padding: 10px;
      .van-image {
        vertical-align: top;
        width: 40px;
        height: 40px;
      }
      .chat-pao {
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 38px;
        border: 0.5px solid #c2d9ea;
        border-radius: 4px;
        position: relative;
        padding: 0 10px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 14px;
        color: #333;
        &::before {
          content: "";
          width: 10px;
          height: 10px;
          position: absolute;
          top: 12px;
          border-top: 0.5px solid #c2d9ea;
          border-right: 0.5px solid #c2d9ea;
          background: #e0effb;
        }
      }
      .chat-pao.my {
        background-color: #9eea6a;
        &::before {
          content: "";
          background: #9eea6a;
        }
      }
    }
  }
  .chat-item.right {
    text-align: right;
    .chat-pao {
      margin-left: 0;
      margin-right: 15px;
      &::before {
        right: -6px;
        transform: rotate(45deg);
      }
    }
  }
  .chat-item.left {
    text-align: left;
    .chat-pao {
      margin-left: 15px;
      margin-right: 0;
      &::before {
        left: -5px;
        transform: rotate(-135deg);
      }
    }
  }
  .reply-container {
    position: fixed;
    left: 0;
    bottom: 0;
    height: 44px;
    width: 100%;
    background: #f5f5f5;
    z-index: 9999;
  }
}
</style>
